package com.mytijian.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mytijian.openapi.adapter.dianping.result.DzResult;
import com.mytijian.openapi.adapter.dianping.service.DzopenAdapterService;

/**
 * 大众点评、美团接口适配。
 * 
 * controller不允许写任何业务相关的东西
 * 
 * @author clozz
 *
 */
@RestController
public class DzopenController {

	@Autowired
	private DzopenAdapterService dzopenAdapterService;

	// 下单请求
	@RequestMapping("/dzopen/book/startbook")
	public DzResult<?> startbook(HttpServletRequest request) throws Exception {
		return dzopenAdapterService.startbook(convertMap(request.getParameterMap()));
	}

	// 撤单
	@RequestMapping("/dzopen/book/cancelbook")
	public DzResult<?> cancelbook(HttpServletRequest request) throws Exception {
		return dzopenAdapterService.cancelbook(convertMap(request.getParameterMap()));
	}

	// 查询核销状态
	@RequestMapping("/dzopen/book/query/consumestatus")
	public DzResult<?> consumestatus(HttpServletRequest request) {
		return dzopenAdapterService.consumestatus(convertMap(request.getParameterMap()));
	}

	// 修改预定时间
	@RequestMapping("/dzopen/book/modifybooktime")
	public DzResult<?> modifybooktime(HttpServletRequest request) {
		return dzopenAdapterService.modifybooktime(convertMap(request.getParameterMap()));
	}

	// 批量获取库存
	@RequestMapping("/dzopen/book/query/batch/remaincountbytime")
	public DzResult<?> remaincountbytime(HttpServletRequest request) {
		return dzopenAdapterService.remaincountbytime(convertMap(request.getParameterMap()));
	}

	// 授权回调
	@RequestMapping("/dzopen/oauth/callback")
	public DzResult<?> auth(HttpServletRequest request) {
		return dzopenAdapterService.oauthcallback(convertMap(request.getParameterMap()));
	}

	private Map<String, String> convertMap(Map<String, String[]> reqMap) {
		Map<String, String> map = new HashMap<>();
		for (String key : reqMap.keySet()) {
			String[] values = reqMap.get(key);
			if (values != null && values.length > 0) {
				map.put(key, values[0]);
			} else {
				map.put(key, "");
			}
		}
		return map;
	}
}
